package com.labirynt.impl;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.labirynt.GameObject;
import com.labirynt.MechanicGame;


public  class Monster  extends GameObject  {
	
	
	 private Bitmap mBmpMonster;
	 private Paint textPaint;
	private int newXpos;
	private int newYpos;
	
		public Monster(String name,String animBmp, int x, int y,int height, int width, Context ctx) {
			super(name,x, y,(int) x+120,(int) y+132);
			this.Initialize(100,1, 1, false);
			
			LoadAnimation(animBmp, ctx);
			this.innitMechanicGme(10, 15);
			 textPaint = new Paint();
	         textPaint.setARGB(255,255,0,115);
	         textPaint.setTextSize(12);
	         newXpos = 0;
	         newYpos=0;
			
		}

		
		public void LoadAnimation(String animBmp, Context ctx) {
			try
			  {

				this.mBmpMonster=  BitmapFactory.decodeStream(ctx.getAssets().open(animBmp+".png")); 
				} 
			    catch (IOException e) { 
			    	e.printStackTrace(); 
			    }
		}
		@Override
		public void SetPos(int x,int y)
		{
			  newXpos=x;
		      newYpos=y;
			
			
		}
	
	@Override
	public void Update(long gameTime) {
		if (gameTime > frameTimer + fps) {
			frameTimer = gameTime;
			currentFrame += 1;

			if (currentFrame >= numFrames) {
				currentFrame = 0;

				if (!loop)
					dispose = true;

			}

			sRectangle.left = currentFrame * spriteWidth;
			sRectangle.right = sRectangle.left + spriteWidth;
		}
		if(this.getMechanicGame().IsNotDead()){
		if(xPos < newXpos && newXpos-xPos<100 )
			this.xPos+=2;
			else 	if(xPos > newXpos && xPos -newXpos<100)
				this.xPos-=2;
			if(yPos<newYpos && newYpos- yPos<100)
			this.yPos+=2;
			else if(yPos>newYpos &&  yPos-newYpos<100)
				this.yPos-=2;
		}
	}
	
	
	@Override
	 public void draw(Canvas canvas)
	{
		if(this.getMechanicGame().IsNotDead())
		{
			 int width=-120/2;//canvas.getWidth();	
			  int heigh =-132;//canvas.getHeight();
			
					Rect dest = new Rect(width+getXPos(),heigh+ getYPos(), width+getXPos() + spriteWidth,
							heigh+getYPos() + spriteHeight);

					canvas.drawBitmap(mBmpMonster, sRectangle, dest, null);
					Paint RectPaint = new Paint();
					RectPaint.setColor(Color.DKGRAY);		
					canvas.drawRect(width+getXPos()-10, heigh+getYPos(),width+getXPos()+40, heigh+getYPos()-10, RectPaint);
					RectPaint.setColor(Color.CYAN);
					canvas.drawRect(width+getXPos()-5, heigh+getYPos()-2,width+getXPos()+(this.getMechanicGame().iHitPoints-5)/2, heigh+ getYPos()-8, RectPaint);
			        canvas.drawText(obiektName ,width+getXPos()-20,heigh+ getYPos()-25, textPaint);
				
		}
		
	}

	
	




	public Bitmap getMonsterBmp() {
		return animation;
	}




	public void setMonsterBmp(Bitmap monsterBmp) {
		this.animation = monsterBmp;
	}

	 public boolean isCollision(int x,int y)
	  {
		  return  !(( x <this.xPos+this.spriteWidth
		    		&& x>this.xPos)
		    		||( y < this.yPos
		    		&& y >  this.yPos+this.spriteHeight
		    		));
		  
	  }
	  
	  public boolean isCollisionWith(GameObject other)  
	  {
	    return  intersectRect( other);
	  }








}